﻿using BCCommon;
using BCCommon.OAEnums.WorkflowActive;
using BCData.OA.WorkflowActivity.WorkflowActivityStep;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStep;
using BCDto.OA.WorkflowActive.WorkflowActive;
using BCDto.OA.WorkflowActive.WorkflowActivity;
using BCDto.OA.WorkflowActive.WorkflowActivityOperation;
using BCDto.OA.WorkflowActive.WorkflowActivityStep;
using BCDto.OA.WorkflowActive.WorkflowActivityStepFormConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplate;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepButtonConfig;
using BCDto.Sim.Admin.Admin;
using BCDto.Website.User;
using BCService.OA.WorkflowTemplate.WorkflowTemplate;
using BCService.Utils;
using BCWorkflowEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.OA.WorkflowActivity
{
    public class WorkflowActivityService : IWorkflowActivityService
    {
        private readonly IWorkflowEngine workflowEngine;
        private readonly IWorkflowTemplateService workflowTemplateService;
        private readonly IWorkflowTemplateStepData workflowTemplateStepData;
        public WorkflowActivityService(IWorkflowEngine workflowEngine, IWorkflowTemplateService workflowTemplateService, IWorkflowTemplateStepData workflowTemplateStepData)
        {
            this.workflowEngine = workflowEngine;
            this.workflowTemplateService = workflowTemplateService;
            this.workflowTemplateStepData = workflowTemplateStepData;
        }

        public WorkflowActivityDto InitActivity(long workflowTemplateId, AdminDto adminDto, UserDto userDto)
        {
            WorkflowTemplateDto workflowTemplateDto = this.workflowTemplateService.Get(workflowTemplateId);

            return this.workflowEngine.InitWorkflowActivityInstance(workflowTemplateDto, adminDto, userDto).As<WorkflowActivityDto>();
        }

        public List<WorkflowActivityStepFormConfigDto> SubmitForm(List<WorkflowActivityStepFormConfigRequestDto> requestDtos, AdminDto adminDto, UserDto userDto)
        {
            return this.workflowEngine.SubmitWorkflowActivityStepForm(requestDtos, adminDto, userDto);
        }

        public WorkflowActivityDto Execution(WorkflowActivityOperationRequestDto workflowActivityOperationRequestDto)
        {
            return this.workflowEngine.Execution(workflowActivityOperationRequestDto);
        }

        public WorkflowActivityStepDto UpdateStepStatusToActivity(WorkflowActivityStepPutDto putDto, AdminDto adminDto, UserDto userDto)
        {
            return this.workflowEngine.UpdateStepStatusToActivity(putDto,adminDto,userDto);
        }

        public WorkflowActivityStepDto UpdateStepStatusToComplete(WorkflowActivityStepPutDto putDto)
        {
            return this.workflowEngine.UpdateStepStatusToComplete(putDto);
        }

        public WorkflowActivityStepDto UpdateStepButtonShowCondition(WorkflowActivityStepButtonConditionPutDto putDto)
        {
            return this.workflowEngine.UpdateStepButtonShowCondition(putDto);
        }

        public WorkflowActivityDto Get(long workflowActivityId)
        {
            var activityDto = this.workflowEngine.GetEntity(workflowActivityId);
            if (activityDto == null)
            {
                throw new ArgumentException("此任务不存在!");
            }
            var activityStepList = this.workflowEngine.GetActivityStepList(workflowActivityId);
            var templateStepList = this.workflowTemplateStepData.GetList(activityDto.TemplateCode).GetAwaiter().GetResult();
            activityDto.Steps = activityStepList.ToList();
            foreach(var item in templateStepList)
            {
                if(!activityDto.Steps.Any(p=>p.TemplateStepCode == item.Id))
                {
                    WorkflowActivityStepDto stepDto = new WorkflowActivityStepDto();
                    stepDto.TemplateCode = item.TemplateCode;
                    stepDto.TemplateName = item.TemplateName;
                    stepDto.WorkflowCode = activityDto.WorkflowCode;
                    stepDto.WorkflowName = activityDto.WorkflowName;
                    stepDto.TemplateStepCode = item.Id;
                    stepDto.NodeName = item.NodeName;
                    stepDto.NodeType = item.NodeType;
                    stepDto.StepType = OAActivityStepType.Created;
                    stepDto.Step = item.Step;
                    stepDto.StepStatus = OAActivityStepStatus.Pending;
                    stepDto.StepOrder = activityDto.Steps.Count + 1;
                    activityDto.Steps.Add(stepDto);
                }
            }
            return activityDto;
        }

        public IEnumerable<WorkflowActivityStepFormConfigDto> GetActivityStepFormConfigList(long workflowActivityId)
        {
            return this.workflowEngine.GetActivityStepFormConfigList(workflowActivityId);
        }

        public IEnumerable<WorkflowTemplateStepButtonConfigDto> GetActivityStepButtonList(string templateStepCode)
        {
            return this.workflowEngine.GetActivityStepButtonList(templateStepCode);
        }

        public IEnumerable<WorkflowActivityStepDto> GetActivityStepList(long workflowActivityId)
        {
            return this.workflowEngine.GetActivityStepList(workflowActivityId);
        }

        public PageableList<WorkflowActivityViewDto> PendingHandleTaskQuery(AdminDto adminDto, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            return this.workflowEngine.PendingHandleTaskQuery(adminDto.AdminId, start, end, sortName, ascending, pageIndex, pageSize);
        }

        public PageableList<WorkflowActivityDto> Query(AdminDto adminDto, UserDto userDto, OAActivityStatus activityStatus, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            return this.workflowEngine.Query(adminDto?.AdminId, userDto?.UserId, activityStatus, start, end, sortName, ascending, pageIndex, pageSize);
        }

    }
}
